<?php
/**
 * User: hong
 * Date: 2023-09--21
 * 测试接口
 */

namespace app\api\controller\wechart;

use app\api\controller\wechart\Base as BaseController;
use app\api\service\Order;
use app\common\model\ActivityConfig;
use app\common\model\CardFans;
use app\common\model\CouponFans;
use app\common\model\Device;
use app\common\model\Fans;
use think\Db;
use think\Request;

class Test extends BaseController
{
    /**
     * 随机立减功能测试
     */
    public function ChargeDiscount(Request $request)
    {
        $param = $request->param();
        $deviceInfo = Device::where("group_id", $param["group_id"]??3)->find();
        Fans::vipLevelByUser($this->userInfo);
        $ret = ActivityConfig::ChargeDiscount($this->userInfo, $deviceInfo, $param["pay_price"]??5);
        success($ret);
    }

    /**
     * 订单结束功能测试
     */
    public function OrderEnd(Request $request)
    {
        $param = $request->param();
        if(!empty($param["order_id"])){
            Db::table("yun_order_info")->where("order_id", $param["order_id"])->delete();
        }elseif(!empty($param["order_no"])){
            $param["order_id"] = Db::table("yun_electricity_orders")->where("order_no", $param["order_no"])->value("order_id");
        }
        if(empty($param["order_id"])){
            raise([], "订单号错误");
        }
        (new Order())->orderEnd(['device_num'=>$param["device_num"]??'383633343930303638343833363637','plug_num'=>$param["plug_num"]??1,'order_id'=>$param["order_id"]]);
        $ret = Db::table("yun_order_info")->where("order_id", $param["order_id"])->find();
        success($ret);
    }

    /**
     * 电卡发送
     */
    public function CardSend(Request $request)
    {
        $param = $request->param();
        $ret = CardFans::send($this->userInfo["fans_id"], $param["card_id"]);
        success($ret);
    }

    /**
     * 加电
     */
    public function ChargeAddElectric(Request $request)
    {
        $param = $request->param();
        $model = Db::table("yun_electricity_orders")->where("order_status", 10)->where("group_id", 3);
        if(isset($param["fans_id"])){
            $model->where("uid", $param["fans_id"]);
        }elseif(isset($param["phone"])){
            $u = Fans::where("phone", $param["phone"])->find();
            $model->where("uid", $u["fans_id"]);
        }else{
            raise([], "没有指定用户");
        }
        $order = $model->order("order_id", "DESC")->find();
        if(!$order){
            raise([], "订单错误");
        }
        Db::table("yun_electricity_orders")->where("order_id", $order["order_id"])->update(["electric"=>$param["electric"]]);
        success([]);
    }

    /**
     * 清理vip
     */
    public function VipClear(Request $request)
    {
        $param = $request->param();
        if(isset($param["fans_id"])){
            Fans::where("fans_id", $param["fans_id"])->update(["vip_deadline" => '2022-03-30 23:59:59', "vip_current_end" => '2023-03-30', "vip_cycle" => '[]']);
        }elseif(isset($param["phone"])){
            Fans::where("phone", $param["phone"])->update(["vip_deadline" => '2022-03-30 23:59:59', "vip_current_end" => '2023-03-30', "vip_cycle" => '[]']);
        }else{
            raise([], "没有指定用户");
        }
        success([]);
    }

    /**
     * 清理vip
     */
    public function CouponClear(Request $request)
    {
        $param = $request->param();
        if(isset($param["fans_id"])){
        }elseif(isset($param["phone"])){
            $u = Fans::where("phone", $param["phone"])->find();
            if($u) $param["fans_id"] = $u["fans_id"];
        }
        if(empty($param["fans_id"])){
            raise([], "没有指定用户");
        }
        CouponFans::where("fans_id", $param["fans_id"])->where("status", 0)->update(["status" => 1]);
        success([]);
    }
}